//
//  ChangePwd.m
//  MUBATE
//
//  Created by sunbin on 2017/2/18.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "ChangePwd.h"

@interface ChangePwd ()<UITextFieldDelegate>

@end

@implementation ChangePwd
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = LocalizedString(@"Change Password");
    
}

- (void)viewDidLoad {
    [super viewDidLoad];


    [self initView];
    
}

- (void)initView{
    self.row = 3;
    self.headTitle = @"请确认两次输入的密码保持一致,密码为8位以上字符需数字字母组合";
    self.isShowHead = YES;

    [_enterNewPwd leftViewModeWithConstrainedToWidth:100.f text:LocalizedString(@"new Pwd :") fon:App_textLeftViewFon setFont:App_textFon leftTextColor:[UIColor appLeftTextColor] bodyTextColor:[UIColor aPPBodyTextColor] PlaceholderColor:[UIColor appPlaceholderColor] setPlaceholder:nil isLaunch:NO];

    [_resetPwd leftViewModeWithConstrainedToWidth:100.f text:LocalizedString(@"confirm pwd :") fon:App_textLeftViewFon setFont:App_textFon leftTextColor:[UIColor appLeftTextColor] bodyTextColor:[UIColor aPPBodyTextColor] PlaceholderColor:[UIColor appPlaceholderColor] setPlaceholder:nil isLaunch:NO];

    [NHUtils setButton:_confirmBtn isLaunch:NO setTitle:LocalizedString(@"confirm") setLayerCornerRadius:0 setFont:15.f];
    _confirmBtn.enabled = NO;

}

// 输入监听变化
- (IBAction)editingChanged:(UITextField *)sender {
    
    _confirmBtn.enabled = (![NHUtils isBlankString:_enterNewPwd.text] && ![NHUtils isBlankString:_resetPwd.text]) && ([_enterNewPwd.text checkPassWord] && [_resetPwd.text checkPassWord]);
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_confirmBtn.isEnabled) {
        [self confirmEven:_confirmBtn];
    }else{
        [_resetPwd becomeFirstResponder];
    }
    return YES;
}

- (IBAction)confirmEven:(UIButton *)sender {
    WEAKSELF;
    [weakSelf.view endEditing:YES];
    [weakSelf.navigationController.view showLoading:@"正在修改"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.navigationController.view hideHUD];
        pwdErrorType ty = [NHUtils enterPwd:_enterNewPwd.text resentPwd:_resetPwd.text];
        if (ty == NOError) {
            //格式正确
            NSDictionary *parameters = @{
                                         @"uid"     :@([YConfig getOwnID]),
                                         @"password":[weakSelf.enterNewPwd.text md5String] ,
                                         @"newpass" :[weakSelf.resetPwd.text md5String] ,
                                         @"sign"    :SIGN};

            [weakSelf chagePwd:parameters];
            return;
        }else{
            //错误 原因只有一个两次输入密码不相同
            [weakSelf pwdErrorInfor:ty==FormatError?@"密码格式错误":@"两次输入密码不相同"];
            return;
        }
    });

}

- (void)chagePwd:(NSDictionary *)parameters{
    WEAKSELF;
    [ZL_NetworkHelper POST:EDITPWD parameters:parameters success:^(id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSString *res = IF_NULL_TO_STRING([dic objectForKey:@"res"]);
        if ([res isEqualToString:@"1"]) {
            // 保存密码
            [weakSelf savePwd];
        }else{
            // msg提示信息
            NSString *msg = IF_NULL_TO_STRING([dic objectForKey:@"msg"]);
            NSLog(@"msg===%@" ,msg);
            [weakSelf pwdErrorInfor:msg];
        }
        
    } failure:^(NSError *error) {
        // msg提示信息 error.localizedDescription
        [weakSelf pwdErrorInfor:error.localizedDescription];
    }];

}

- (void)pwdErrorInfor:(NSString *)infor{
    // 只有密码输入不相同情况
    [NHUtils alertAction:@selector(errorToClear) alertControllerWithTitle:@"错误" Message:[infor stringByAppendingString:@",请重新输入"] Vctl:self Cancel:NO];
}
- (void)errorToClear{
    _resetPwd.text = nil;
    [_resetPwd becomeFirstResponder];
    _confirmBtn.enabled = NO;
}

// 保存密码
- (void)savePwd{
    WEAKSELF;
    //修改成功
    //获取原始密码修改保存
    NSString *account = [[YConfig getOwnAccountAndPassword] firstObject];
    NSString *pwd     = [[YConfig getOwnAccountAndPassword] lastObject];
    // 删除旧的密码 保存新的密码
    [SSKeychain deletePasswordForService:@"service" account:pwd];
    [YConfig saveOwnAccount:account andPassword:[weakSelf.resetPwd.text md5String]];

    // 返回上一视图控制器 提示修改成功
    [weakSelf.navigationController popViewControllerAnimated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
